package van.planifolia.worddemo;

/**
 * Created by Intellij IDEA<br>
 *
 * @author Planifolia.Van
 * @version 1.0
 * @date 2022/11/1 18:14
 */
public class DeadLock {
    public static void main(String[] args) {
        // 分别创建口红类与镜子类对象
        Lipstick lipstick = new Lipstick();
        Mirror mirror = new Mirror();
        //然后起两个线程代表两个女生
        new Thread(()->{
            // 小红刚开始占有口红
            synchronized (lipstick){
                System.out.println("小红拿到了口红");
                // 需要另一间物品才能化妆
                synchronized (mirror){
                    System.out.println("小红拿到了镜子");
                    System.out.println("小红化妆完毕，归还物品");
                }
            }
        },"小红").start();
        new Thread(()->{
            // 小玉刚开始占有镜子
            synchronized (mirror){
                System.out.println("小玉拿到了镜子");
                // 需要另一间物品才能化妆
                synchronized (lipstick){
                    System.out.println("小玉拿到了口红");
                    System.out.println("小玉化妆完毕，归还物品");
                }
            }
        },"小红").start();
    }
}
class Lipstick{}
class Mirror{}

